<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/1/16
 * Time: 16:24
 */
namespace app\mtsuperadmin\controller;

use app\api\model\MtCouponClass;
use app\api\model\MtCouponInfo;
use app\command\controller\Common;
use app\command\controller\Generate;
use app\util\ReturnCode;

class AdminSysActivity extends Base
{
    public function createaActivity(){

    }

    /**
     * 生成优惠券
     * @return mixed
     */
    public function createCoupon(){
        $adminID = $this->request->param("adminID");
        $rule = [
            "couponCondition" => "require|number",
            "couponType" => "require|number",
            "conponNum" => "require|number",
            "conponStart" => "require",
            "conponEnd" => "number",
        ];
        $msg = [];
        $houseData = $this->request->post();
        $result = (new Common())->VerifiString($rule,$msg,$houseData);
        if($result['code'] == 2){
            return $this->bindError(ReturnCode::PARAM_INVALID,$result['msg']);
        }
        if($houseData['couponType'] == 1){
            if(!$houseData['couponDiscount']) return $this->bindError(ReturnCode::PARAM_INVALID,"缺少参数折扣");
        }elseif ($houseData['couponType'] == 2){
            if(!$houseData['couponValue']) return $this->bindError(ReturnCode::PARAM_INVALID,"缺少参数减免金额");
        }
        $houseData['cTime'] = time();
        $houseData['conponStart'] = strtotime($houseData['conponStart']);  // 开始时间
        $houseData['conponEnd'] = strtotime($houseData['conponEnd']); // 结束时间
        $houseData['conponTerm'] = strtotime($houseData['conponEnd']) - strtotime($houseData['conponStart']); // 使用期限
        $res = (new MtCouponClass())->insert($houseData,true);
        if($res){
            $couponClassID = (new MtCouponClass())->getLastInsID();
            $generateState = $this->generateCoupon($houseData['conponNum'],$houseData['conponStart'],$houseData['conponEnd'],$couponClassID);
            if(!$generateState){
                return $this->error(ReturnCode::DB_SAVE_ERROR,"优惠券生成失败");
            }
            return $this->success("优惠券生成成功");
        }else{
            return $this->error(ReturnCode::DB_SAVE_ERROR,"class存储失败");
        }
    }

    /**
     * 优惠券生成
     * @param $num
     * @param $drawTime
     * @param $couponEnd
     * @param $couponClassID
     * @return bool
     */
    public function generateCoupon($num,$drawTime,$couponEnd,$couponClassID){
        if(!$num || !$drawTime || !$couponEnd || $couponClassID){
            return false;
        }
        $coufonArr = (new Generate())->generate_promotion_code($num);
        $confonCont = [];
        foreach ($coufonArr as $k => $v){
            $confonCont[$k]['couponNo'] = $v;
            $confonCont[$k]['drawTime'] = $drawTime;
            $confonCont[$k]['couponEnd'] = $couponEnd;
            $confonCont[$k]['couponClassID'] = $couponClassID;
        }
        $res = (new MtCouponInfo())->insertAll($confonCont,true);
        if(!$res){
            return false;
        }
        return true;
    }
}